/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * V1ModAcontAcept.java
 *
 * Created on 14-may-2012, 12:40:47
 */
package practtica4;

import BBDD.GestorBBDD;
import DTO.Acontecimiento;
import DTO.Fichero;
import DTO.Personaje;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author usuario_local
 */
public class V1ModAcontAcept extends javax.swing.JFrame {

    /** Creates new form V1ModAcontAcept */
   GestorBBDD gestor=new GestorBBDD();
    private Acontecimiento acont;
    public V1ModAcontAcept() {
        initComponents();
    }

    V1ModAcontAcept(Acontecimiento acont) {
        this.acont=acont;
        initComponents();
        cargarrEtiquetas();
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        botonDescr2 = new javax.swing.JTextField();
        botonFechaIni = new javax.swing.JTextField();
        botonAceptar = new javax.swing.JButton();
        botonFechaFin = new javax.swing.JTextField();
        botonNombre = new javax.swing.JTextField();
        botonId = new javax.swing.JTextField();
        labelFechaIni = new javax.swing.JLabel();
        labelNombre = new javax.swing.JLabel();
        labelDesc = new javax.swing.JLabel();
        labelFechaFin = new javax.swing.JLabel();
        botonEliminar = new javax.swing.JButton();
        labelId = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        botonfich = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        botonDescr2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonDescr2ActionPerformed(evt);
            }
        });

        botonAceptar.setText("Aceptar");
        botonAceptar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                botonAceptarMouseClicked(evt);
            }
        });

        labelFechaIni.setText("Fecha inicio");

        labelNombre.setText("nombre");

        labelDesc.setText("descripción");

        labelFechaFin.setText("Fecha fin");

        botonEliminar.setText("Eliminar");
        botonEliminar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                botonEliminarMouseClicked(evt);
            }
        });

        labelId.setText("id");

        jLabel1.setText("Ficheros");

        botonfich.setColumns(20);
        botonfich.setRows(5);
        jScrollPane1.setViewportView(botonfich);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(botonEliminar)
                .addGap(0, 260, Short.MAX_VALUE)
                .addComponent(botonAceptar)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(93, 93, 93)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelId)
                    .addComponent(labelFechaFin)
                    .addComponent(labelFechaIni)
                    .addComponent(labelDesc)
                    .addComponent(labelNombre)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(botonId)
                    .addComponent(botonNombre)
                    .addComponent(botonDescr2)
                    .addComponent(botonFechaIni)
                    .addComponent(botonFechaFin)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(33, 33, 33))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(botonId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(botonNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(labelNombre))
                        .addGap(28, 28, 28)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(botonDescr2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(labelDesc))
                        .addGap(23, 23, 23)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(botonFechaIni, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(labelFechaIni))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(botonFechaFin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(labelFechaFin))
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 71, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(botonAceptar)
                            .addComponent(botonEliminar))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labelId)
                        .addGap(214, 214, 214)
                        .addComponent(jLabel1)
                        .addGap(0, 0, Short.MAX_VALUE))))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void botonDescr2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonDescr2ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_botonDescr2ActionPerformed

    private void botonAceptarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_botonAceptarMouseClicked
        // TODO add your handling code here:
        // acont.setId(Integer.parseInt(this.botonId.getText()));
        acont.setNombre(this.botonNombre.getText());
        acont.setDescripcion(this.botonDescr2.getText());
       
       
        String fechai =  this.botonFechaIni.getText(); 
        String fechaf =  this.botonFechaFin.getText(); 
        java.text.SimpleDateFormat formato = new java.text.SimpleDateFormat("yyyy/MM/dd"); 
        java.util.Date fechaDateN = null;
        java.util.Date fechaDateM = null; 
        try {
            fechaDateN = formato.parse(fechai);
            fechaDateM = formato.parse(fechaf);
        } catch (ParseException ex) {
            Logger.getLogger(V1ModPersoAcept.class.getName()).log(Level.SEVERE, null, ex);
        }
        acont.setFechaIni(fechaDateN);
        acont.setFechaFin(fechaDateM);
        String fich=this.botonfich.getText();
        ArrayList<Fichero> fi=acont.getFich();
        int fin=0;
        int ini=0;
        int i=0;
        while(fin<fich.length()){
            ini=fin+1;
            fin= fich.indexOf('\n', ini);
            fi.get(i).setNombre(fich.substring(ini, ini));
            i++;
        }
        acont.setFich(fi);
       try {
            gestor.guardarAcontecimiento(acont);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(V1ModPersoAcept.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(V1ModPersoAcept.class.getName()).log(Level.SEVERE, null, ex);
        }
        V1 v1=new V1();
        v1.setVisible(true);
            this.dispose();
        
        
    }//GEN-LAST:event_botonAceptarMouseClicked

    private void botonEliminarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_botonEliminarMouseClicked
        // TODO add your handling code here:
        try {
            gestor.eliminarAcontecimiento(acont);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(V1ModPersoAcept.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(V1ModPersoAcept.class.getName()).log(Level.SEVERE, null, ex);
        }
        V1 v1=new V1();
         v1.setVisible(true);
            this.dispose();
    }//GEN-LAST:event_botonEliminarMouseClicked

    /**
     * @param args the command line arguments
     */
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonAceptar;
    private javax.swing.JTextField botonDescr2;
    private javax.swing.JButton botonEliminar;
    private javax.swing.JTextField botonFechaFin;
    private javax.swing.JTextField botonFechaIni;
    private javax.swing.JTextField botonId;
    private javax.swing.JTextField botonNombre;
    private javax.swing.JTextArea botonfich;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel labelDesc;
    private javax.swing.JLabel labelFechaFin;
    private javax.swing.JLabel labelFechaIni;
    private javax.swing.JLabel labelId;
    private javax.swing.JLabel labelNombre;
    // End of variables declaration//GEN-END:variables

    private void cargarrEtiquetas() {
        String id=acont.getId()+"";
       this.botonId.setText(id);
       this.botonNombre.setText(acont.getNombre());
       this.botonDescr2.setText(acont.getDescripcion());
       this.botonFechaIni.setText(acont.getFechaIni().toString());
       this.botonFechaFin.setText(acont.getFechaFin().toString());
       String fich="";
        int i=0;
        while(i<acont.getFich().size()){
            fich+= acont.getFich().get(i).getNombre()+'\n';
            i++;
        }
        this.botonfich.setText(fich);
    }
}
